<?php

class Default_Model_RowMapper {
protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if(is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
	}
	
	public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Default_Model_DbTable_Row');
        }
        return $this->_dbTable;
    }
    
	public function save(Default_Model_Seat $row)
    {
        $data = array(
        	'isActivated'	  => $row->setIsActivated(),
        	'rowNr'  => $row->getRowNr(),
        	'seatmap'  => $row->getSeatmap(),
        	'position'	=> $row->getPosition(),
        	'xpos'	=> $row->getXpos(),
        	'ypos'	=> $row->getYpos(),
        );

        if (null === ($id = $row->getRowId())) {
            unset($data['rowId']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('rowId = ?' => $id));
        }
    }
    
	public function find($id, Default_Model_Row $seatrow)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $seatrow->setRowId($row->rowId)
                  ->setIsActivated($row->isActivated)
                  ->setRowNr($row->rowNr)
                  ->setSeatmap($row->seatmap)
                  ->setPosition($row->position)
                  ->setXpos($row->xpos)
                  ->setYpos($row->ypos);
    }
	
	public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Default_Model_Seat();
            $entry->setRowId($row->rowId)
                  ->setIsActivated($row->isActivated)
                  ->setRowNr($row->rowNr)
                  ->setSeatmap($row->seatmap)
                  ->setPosition($row->position)
                  ->setXpos($row->xpos)
                  ->setYpos($row->ypos)
                  ->setMapper($this);
            $entries[] = $entry;
        }
        return $entries;
    }
}

?>